load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Apple cc toolchain needs to be loaded before regular cc toolchain or else Apple will just use regular cc toolchain
http_archive(
    name = "build_bazel_apple_support",
    sha256 = "b53f6491e742549f13866628ddffcc75d1f3b2d6987dc4f14a16b242113c890b",
    url = "https://github.com/bazelbuild/apple_support/releases/download/1.17.1/apple_support.1.17.1.tar.gz",
)

load(
    "@build_bazel_apple_support//lib:repositories.bzl",
    "apple_support_dependencies",
)

apple_support_dependencies()

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

load("//bazel/platforms:local_config_platform.bzl", "setup_local_config_platform")
load("//bazel/toolchains:mongo_toolchain.bzl", "toolchain_download")

http_archive(
    name = "bazel_clang_tidy",
    sha256 = "f77f7f63fc43b6f7dba23f807132e24c36110d481826685ce49f38a04058c4ea",
    strip_prefix = "bazel_clang_tidy-1.5",
    urls = [
        # Implements retry by relisting each url multiple times to be used as a failover.
        # TODO(SERVER-86719): Re-implement http_archive to allow sleeping between retries
        "https://github.com/mongodb-forks/bazel_clang_tidy/archive/refs/tags/v1.5.tar.gz",
        "https://github.com/mongodb-forks/bazel_clang_tidy/archive/refs/tags/v1.5.tar.gz",
        "https://github.com/mongodb-forks/bazel_clang_tidy/archive/refs/tags/v1.5.tar.gz",
        "https://github.com/mongodb-forks/bazel_clang_tidy/archive/refs/tags/v1.5.tar.gz",
        "https://github.com/mongodb-forks/bazel_clang_tidy/archive/refs/tags/v1.5.tar.gz",
    ],
)

setup_local_config_platform(name = "internal_platforms_do_not_use")

toolchain_download(name = "mongo_toolchain")

http_archive(
    name = "windows_sasl",
    build_file_content = """
package(default_visibility = ["//visibility:public"])

filegroup(
    name = "includes",
    srcs = select({
        "@platforms//os:windows": glob(["include/**/*.h"]),
        "//conditions:default": [],
    }),
)

filegroup(
    name = "libraries",
    srcs = select({
        "@platforms//os:windows": glob(["lib/**/*"]),
        "//conditions:default": [],
    }),
)
    """,
    sha256 = "3e22e2b16f802277123590f64dfda44f1c9c8a2b7e758180cd956d8ab0965817",
    url = "https://s3.amazonaws.com/boxes.10gen.com/build/windows_cyrus_sasl-2.1.28.zip",
)

register_toolchains("@mongo_toolchain//:mongo_toolchain")

load("//bazel/toolchains:python_toolchain.bzl", "setup_mongo_python_toolchains")

[register_toolchains(toolchain) for toolchain in setup_mongo_python_toolchains()]

load("//bazel/coverity:coverity_toolchain.bzl", "coverity_toolchain")

coverity_toolchain(
    name = "rules_coverity",
)

load("@rules_coverity//coverity:repositories.bzl", "rules_coverity_toolchains")

rules_coverity_toolchains()

# Hedron's Compile Commands Extractor for Bazel
# https://github.com/hedronvision/bazel-compile-commands-extractor
http_archive(
    # TODO SERVER-91979
    # the compile commands extractor is tied to bazel version and toolchain resolution
    # so this might need to be update when bazel version is updated.
    name = "hedron_compile_commands",
    sha256 = "d699c5e3a82b811d7051e833b1808358256a9c52285a43e6a3abb76c809ab03f",
    strip_prefix = "bazel-compile-commands-extractor-33658bab23a4858b513d767480b43d3d8fb6a3d1",
    url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/33658bab23a4858b513d767480b43d3d8fb6a3d1.tar.gz",
)

load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")

hedron_compile_commands_setup()

load("@hedron_compile_commands//:workspace_setup_transitive.bzl", "hedron_compile_commands_setup_transitive")

hedron_compile_commands_setup_transitive()

load("@hedron_compile_commands//:workspace_setup_transitive_transitive.bzl", "hedron_compile_commands_setup_transitive_transitive")

hedron_compile_commands_setup_transitive_transitive()

load("@hedron_compile_commands//:workspace_setup_transitive_transitive_transitive.bzl", "hedron_compile_commands_setup_transitive_transitive_transitive")

hedron_compile_commands_setup_transitive_transitive_transitive()

http_archive(
    name = "platforms",
    sha256 = "8150406605389ececb6da07cbcb509d5637a3ab9a24bc69b1101531367d89d74",
    urls = [
        # Implements retry by relisting each url multiple times to be used as a failover.
        # TODO(SERVER-86719): Re-implement http_archive to allow sleeping between retries
        "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
        "https://github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
        "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
        "https://github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
        "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
        "https://github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
        "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
        "https://github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
    ],
)

# We need skylib to be able to use config_setting_group in rule_poetry below
# https://github.com/bazelbuild/bazel-skylib/blob/main/docs/selects_doc.md#selectsconfig_setting_group
http_archive(
    name = "bazel_skylib",
    sha256 = "cd55a062e763b9349921f0f5db8c3933288dc8ba4f76dd9416aac68acee3cb94",
    urls = [
        # Implements retry by relisting each url multiple times to be used as a failover.
        # TODO(SERVER-86719): Re-implement http_archive to allow sleeping between retries
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
    ],
)

# Poetry rules for managing Python dependencies
http_archive(
    name = "rules_poetry",
    sha256 = "dbabea2a3829aa5885ba46d612b269956d0227b503147a5e1a2cfd635165fcb5",
    strip_prefix = "rules_poetry-441f4365f7cafce345083a6325bc6712a2ce5c93",
    urls = [
        # Implements retry by relisting each url multiple times to be used as a failover.
        # TODO(SERVER-86719): Re-implement http_archive to allow sleeping between retries
        "https://github.com/mongodb-forks/rules_poetry/archive/441f4365f7cafce345083a6325bc6712a2ce5c93.tar.gz",
        "https://github.com/mongodb-forks/rules_poetry/archive/441f4365f7cafce345083a6325bc6712a2ce5c93.tar.gz",
        "https://github.com/mongodb-forks/rules_poetry/archive/441f4365f7cafce345083a6325bc6712a2ce5c93.tar.gz",
        "https://github.com/mongodb-forks/rules_poetry/archive/441f4365f7cafce345083a6325bc6712a2ce5c93.tar.gz",
        "https://github.com/mongodb-forks/rules_poetry/archive/441f4365f7cafce345083a6325bc6712a2ce5c93.tar.gz",
    ],
)

load("@rules_poetry//rules_poetry:defs.bzl", "poetry_deps")

poetry_deps()

load("@rules_poetry//rules_poetry:poetry.bzl", "poetry")

poetry(
    name = "poetry",
    lockfile = "//:poetry.lock",
    pyproject = "//:pyproject.toml",
    python_interpreter_target_default = "@py_host//:bin/python3",
    python_interpreter_target_mac = "@py_host//:bin/python3",
    python_interpreter_target_win = "@py_host//:python.exe",
)

http_archive(
    name = "aspect_rules_js",
    sha256 = "630a71aba66c4023a5b16ab3efafaeed8b1a2865ccd168a34611eb73876b3fc4",
    strip_prefix = "rules_js-1.37.1",
    urls = [
        # Implements retry by relisting each url multiple times to be used as a failover.
        # TODO(SERVER-86719): Re-implement http_archive to allow sleeping between retries
        "https://github.com/aspect-build/rules_js/releases/download/v1.37.1/rules_js-v1.37.1.tar.gz",
        "https://github.com/aspect-build/rules_js/releases/download/v1.37.1/rules_js-v1.37.1.tar.gz",
        "https://github.com/aspect-build/rules_js/releases/download/v1.37.1/rules_js-v1.37.1.tar.gz",
        "https://github.com/aspect-build/rules_js/releases/download/v1.37.1/rules_js-v1.37.1.tar.gz",
        "https://github.com/aspect-build/rules_js/releases/download/v1.37.1/rules_js-v1.37.1.tar.gz",
    ],
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
    name = "nodejs",
    node_version = DEFAULT_NODE_VERSION,
)

load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock")

npm_translate_lock(
    name = "npm",
    pnpm_lock = "//:pnpm-lock.yaml",
    verify_node_modules_ignored = "//:.bazelignore",
)

load("@npm//:repositories.bzl", "npm_repositories")

npm_repositories()

# Sub in the system openssl for boringssl since we don't want two implementations of
# ssl in the same address space.
new_local_repository(
    name = "boringssl",
    build_file_content = """
cc_library(
  name = "crypto",
  linkopts = ["-lcrypto"],
  visibility = ["//visibility:public"],
)
cc_library(
  name = "ssl",
  linkopts = ["-lssl"],
  visibility = ["//visibility:public"],
)
""",
    path = "bazel/_openssl_placeholder_for_grpc",
)

# Overloads for the vendored repositories.
#
# WARNING: Don't change the order of the deps() calls and local_repositories.
#          They're read linearly dependencies that come first override later
#          ones. Dependency updates might change the correct order, though it's
#          unlikely. This is obviously a temporary solution and will no longer
#          be necessary once migration to bzlmod is complete.

# Note: rules_python is implicitly loaded with a grpc-compatible version.

local_repository(
    name = "com_google_absl",
    path = "src/third_party/abseil-cpp/dist",
)

local_repository(
    name = "com_google_protobuf",
    path = "src/third_party/protobuf/dist",
)

local_repository(
    name = "com_github_grpc_grpc",
    path = "src/third_party/grpc/dist",
)

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")

grpc_deps()

load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")

grpc_extra_deps()
